#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 4

void panic(char *message)
{
	perror(message);
	exit(EXIT_FAILURE);
}

int main (int argc, char *argv[])
{
	if(argc<2)
		panic("input");
	else if(argv[argc-1][0]=='>') {
		char *file=argv[2]+1;
		int fd= open(file, O_CREAT | O_RDWR | O_TRUNC, 0666);
		for(int i=1; i<argc-1; i++) {
			int count= write(fd, argv[i], strlen(argv[i]));
			if(count<0)
				panic("write");
		}
		char buf[SIZE];
		while(1) {
			int count= read(fd, buf, sizeof(buf));
			if(count==0)
				break;
			write(1, buf, count);
		}
		close(fd);
	}
	else {
		for(int i=1; i<argc; i++) {
			int count =write(1, argv[i], strlen(argv[i]));
			count= write(1, " ", 1);
		}
		printf("\n");
	}
	return 0;
}
